package xyz.yolin.controller;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.yolin.pojo.Admin;
import xyz.yolin.pojo.Question;
import xyz.yolin.pojo.Survey;
import xyz.yolin.service.QuestionService;
import xyz.yolin.service.SurveyService;
import xyz.yolin.utils.MapControl;
import xyz.yolin.utils.SessionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private SurveyService surveyService;

    @RequestMapping("/create")
    @ResponseBody
    public Map<String,Object> create(@RequestBody Question question, HttpServletRequest request) {
        System.out.println("前端获取到的数据"+question);
//        Admin curradmin = SessionUtils.getAdmin(request);
//        System.out.println(curradmin);
        int result=questionService.create(question);
        if (result<=0){
            return MapControl.getInstance().err().getMap();
        }
        return MapControl.getInstance().success().put("id",result).getMap();
    }

    @ResponseBody
    @PostMapping("/delete")
    public Map<String,Object> delete(String ids){
        System.out.println(ids);
        int result = questionService.delete(ids);
        if (result<=0){
            return MapControl.getInstance().err().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(@RequestBody Question question){
        System.out.println("前端发送来的question"+question);
        int result = questionService.update(question);
        if (result<=0){
            return MapControl.getInstance().err().getMap();
        }
        return MapControl.getInstance().success().getMap();
    }
    @PostMapping("/query")
    @ResponseBody
    public Map<String, Object> query(@RequestBody Question question, ModelMap modelMap){
        System.out.println("前端传过来的question:"+question);
        List<Question> list = questionService.query(question);
        Integer count=questionService.count(question);
        return MapControl.getInstance().page(list,count).getMap();
    }

    @RequestMapping("/detail")
    public String detail(Integer id, ModelMap modelMap){
        Question question= questionService.detail(id);
        modelMap.addAttribute("question",question);
        return "survey/update";
    }
}
